/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SLE_ADAPTER_MANAGER_H
#define SLE_ADAPTER_MANAGER_H

#include <memory>
#include "refbase.h"
#include "sle_adapter_state_machine.h"
#include "sle_host_service.h"
#include "sle_utils_base_observer_list.h"
#include "sle_device_config.h"
#include "sle_error_code.h"

namespace OHOS {
namespace NearLink {
namespace SleStandard {
class ISleAdapterManagerCallback {
public:
    virtual ~ISleAdapterManagerCallback() = default;
    virtual void OnSleStateChange(const SleDeviceState state) = 0;
};

class SleAdapterManager {
public:
    static SleAdapterManager *GetInstance();
    SleErrorCode Start() const;
    SleErrorCode Enable() const;
    SleErrorCode Disable() const;
    SleErrorCode GetState(int32_t &state) const;
    SleErrorCode RegisterAdapterCallback(ISleAdapterManagerCallback &callback) const;
    SleErrorCode UnregisterAdapterCallback(ISleAdapterManagerCallback &callback) const;
    void OnSleStateChange(const SleDeviceState state) const;

private:
    class SleAdapterManageImpl {
    public:
        class AdapterCallbackImplement : public IAdapterCallback {
        public:
            AdapterCallbackImplement(SleAdapterManager::SleAdapterManageImpl &impl) : impl_(impl){};
            ~AdapterCallbackImplement() = default;

            void OnSleEnableResult(SleErrorCode errCode) override
            {
                impl_.EnableComming(errCode);
            }

            void OnSleDisableResult(SleErrorCode errCode) override
            {
                impl_.DisableComming(errCode);
            }

        private:
            SleAdapterManager::SleAdapterManageImpl &impl_;
        };

        std::unique_ptr<SleAdapterStateMachine> stateMachine_ = nullptr;
        std::unique_ptr<AdapterCallbackImplement> callbackImpl_ = nullptr;
        SleDeviceState state_ = SLE_HOST_STATE_DISABLED;
        SleBaseObserverList<ISleAdapterManagerCallback> callbacks_;

        void EnableComming(SleErrorCode errCode);
        void DisableComming(SleErrorCode errCode);
        void OnSleStateChange(const SleDeviceState state);

        SleAdapterManageImpl();
        ~SleAdapterManageImpl();
        SLE_DISALLOW_COPY_AND_ASSIGN(SleAdapterManageImpl);
    };

    std::unique_ptr<SleAdapterManageImpl> pimpl_;
    SleAdapterManager();
    ~SleAdapterManager();
    SLE_DISALLOW_COPY_AND_ASSIGN(SleAdapterManager);
};
} // SleStandard
} // NearLink
} // OHOS
#endif // SLE_ADAPTER_MANAGER_H